package cn.msqweb.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.msqweb.entity.Member;
import cn.msqweb.entity.ProdComm;
import cn.msqweb.model.Result;
import cn.msqweb.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

/**
 * 后台会员管理控制层
 */
@Api(tags = "后台会员接口管理")
@RequestMapping("admin/user")
@RestController
public class SysMemberController {

    @Autowired
    private MemberService memberService;


    @ApiOperation("多条件分页查询会员列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:user:page')")
    public Result<Page<Member>> loadSysMemberPage(@ApiParam("页码") @RequestParam Long current,
                                                  @ApiParam("每页显示条数") @RequestParam Long size,
                                                  @ApiParam("会员状态") @RequestParam(required = false) Integer status,
                                                  @ApiParam("昵称") @RequestParam(required = false) String nickName) {
        // 创建分页对象
        Page<Member> page = new Page<>(current,size);
        // 多条件分页查询
        page = memberService.page(page,new LambdaQueryWrapper<Member>()
                        .eq(ObjectUtil.isNotNull(status),Member::getStatus,status)
                        .like(StringUtils.hasText(nickName),Member::getNickName,nickName)
                .orderByDesc(Member::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("根据标识查询会员信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('admin:user:info')")
    public Result<Member> loadMemberInfo(@ApiParam("会员标识") @PathVariable Long id) {
        Member member = memberService.getById(id);
        return Result.success(member);
    }


    @ApiOperation("修改会员帐号状态")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:user:update')")
    public Result<String> modifyMemberStatus(@ApiParam("会员对象") @RequestBody Member member) {
        member.setUpdateTime(new Date());
        boolean updated = memberService.updateById(member);
        return Result.handler(updated);
    }

    @ApiOperation("批量注销会员帐号")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:user:delete')")
    public Result<String> unsubscribeMember(@ApiParam("会员id集合") @RequestBody List<Long> ids) {
        Member member = new Member();
        member.setStatus(-1);
        boolean updated = memberService.update(member, new LambdaUpdateWrapper<Member>()
                .in(Member::getId, ids)
        );
        return Result.handler(updated);
    }
    @GetMapping("getMemberByOpenId")
    public Result<Member> getMemberByOpenId(@RequestParam String openId) {
        Member member = memberService.getOne(new LambdaQueryWrapper<Member>()
                .eq(Member::getOpenId, openId)
        );
        return Result.success(member);
    }

}
